package Thread;

public class Dome4 {
    public static void main(String[] args) {
        Object object = new Object();
        Thread t1 = new Thread(()-> {
            //给线程t1加锁
            for (int i = 0; i < 3; i++) {
                try{
                    Thread.sleep(1000);
                }catch(InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (i<2)
                    System.out.println("t1等待启动....");
                else
                    System.out.println("t1任然在等待中...");
            }

            synchronized(object) {
                //进入等待被唤醒状态
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                    try{
                        Thread.sleep(1000);
                    }catch(InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

            System.out.println("t1结束了");
        });
        t1.start();
        try{
            //main线程等t1线程4s
            t1.join(4000);
        }catch(InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("mian线程启动");
        System.out.println("main线程给t1线程解锁中，请等待...");
        try{
            Thread.sleep(100);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (object) {
            object.notify();
        }

        System.out.println("mian线程结束");
    }
}
